package com.nnxieli.trans.ui.activity

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.widget.CompoundButton
import androidx.recyclerview.widget.LinearLayoutManager
import com.hd.trans.TranslationEntry
import com.hd.trans.config.ThemeProperty
import com.hd.trans.framework.dialog.LanguageItemDecoration
import com.hd.trans.utils.DensityUtils
import com.nnxieli.trans.R
import com.nnxieli.trans.adapter.ColorAdapter
import com.nnxieli.trans.base.BaseActivity
import com.nnxieli.trans.bean.ColorBean
import com.nnxieli.trans.databinding.ActivityStartBinding
import com.nnxieli.trans.util.LimitUserImpl


class StartActivity : BaseActivity<ActivityStartBinding>() {
    private val colorList = arrayListOf(
        ColorBean(
            "青出于蓝还是蓝",
            "#33CCFF"
        ),
        ColorBean(
            "粉青",
            "#00FFCC"
        ),
        ColorBean(
            "土黄",
            "#eeaa11"
        ),
        ColorBean(
            "死亡芭比",
            "#ffc0cb"
        ),
        ColorBean(
            "烈焰红唇",
            "#CC0000"
        ),
        ColorBean(
            "钢铁直男",
            "#333333"
        ),
        ColorBean(
            "老年赭石咖",
            "#845a33"
        ),
    )
    private var colorValue: String = colorList[0].colorValue

    override val layoutId: Int
        get() = R.layout.activity_start

    override fun initView(savedInstanceState: Bundle?) {
        mDataBinding.btnNext.backgroundTintList =
            ColorStateList.valueOf(Color.parseColor(colorValue))
        mDataBinding.btnNext.setOnClickListener {
            TranslationEntry.getInstance().themeProperty = ThemeProperty.Builder()
                .setThemeColor(colorValue)
                .build()
            startActivity(MainActivity::class.java)
        }

        LimitUserImpl.setIsVip(mDataBinding.switchVip.isChecked)
        mDataBinding.switchVip.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
            LimitUserImpl.setIsVip(isChecked)
        })

        initAdapter()
    }

    private fun initAdapter() {
        val adapter = ColorAdapter(colorList)
        mDataBinding.colorRecycler.adapter = adapter
        mDataBinding.colorRecycler.layoutManager = LinearLayoutManager(this)
        mDataBinding.colorRecycler.addItemDecoration(
            LanguageItemDecoration(
                DensityUtils.dip2px(
                    this,
                    5f
                )
            ))

        adapter.setOnItemClickListener { _, name ->
            colorValue = name
            mDataBinding.btnNext.backgroundTintList =
                ColorStateList.valueOf(Color.parseColor(name))
        }
    }

}